<?php

namespace app\common;

use app\model\Config;
use app\tool\Http;
use app\tool\TLSSigAPIV2;

class TencentIm
{
	
	private string $usersig;
	private string $url='https://console.tim.qq.com';
	/**
	 * @var mixed
	 */
	private $app_id;
	/**
	 * @var mixed
	 */
	private $identifier;
	
	private static $instance = null;
	// 禁止被实例化
	private function __construct()
	{
		$identifier='administrator';
		$row = Config::getFieldByName('tencent_cloud', 'value');
		$row = json_decode($row);
		$this->usersig= (new TLSSigAPIV2($row->imAppid,$row->imKey))->genUserSig($identifier);
		$this->app_id= $row->imAppid;
		$this->identifier= $identifier;
	}
	
	// 禁止clone
	private function __clone()
	{
	
	}
	
	public static function getInstance(): object
	{
		if (empty(self::$instance)) {
			self::$instance = new self();
		}
		return self::$instance;
	}
	

    /**
     * 发送消息  uid不填 以管理员身份发送消息
     * @return bool|string
     */
    function send_msg($uid='',$toUid='',$MsgBody=[],$CloudCustomData='',$SendMsgControl=[])
    {
        $url=$this->url.'/v4/openim/sendmsg?sdkappid='.$this->app_id.'&identifier='.$this->identifier.'&usersig='.$this->usersig.'&random='.mt_rand(0,4294967295).'&contenttype=json';
        $data=[
            'SyncOtherMachine'=>1,
            'To_Account'=>'hk_'. $toUid,
            'MsgRandom'=>mt_rand(0,4294967295),
            'MsgBody'=>$MsgBody,

        ];
        if (!empty($uid)){
            $data['From_Account']='hk_'. $uid;
        }
        if (!empty($SendMsgControl)){
            $data['SendMsgControl']=$SendMsgControl;
        }
        if (!empty($CloudCustomData)){
            $data['CloudCustomData']=$CloudCustomData;
        }
        return Http::post($url,json_encode($data));
    }

    /**
     * 导入账号
     * @param $uid
     * @return mixed|string
     */
    function account_import($uid,$Nick,$FaceUrl)
    {
        $url=$this->url.'/v4/im_open_login_svc/account_import?sdkappid='.$this->app_id.'&identifier='.$this->identifier.'&usersig='.$this->usersig.'&random='.mt_rand(0,4294967295).'&contenttype=json';
        $data=[
            'UserID'=>'hk_'. $uid,
            'Nick'=>$Nick,
            'FaceUrl'=>$FaceUrl
        ];
        return Http::post($url,json_encode($data));
    }
	

    /**
     * 设置资料
     * @param $uid
     * @param $ProfileItem
     * @return mixed|string
     */
    function portrait_set($uid,$ProfileItem)
    {
        $url=$this->url.'/v4/profile/portrait_set?sdkappid='.$this->app_id.'&identifier='.$this->identifier.'&usersig='.$this->usersig.'&random='.mt_rand(0,4294967295).'&contenttype=json';
        $data=[
            'From_Account'=>'hk_'. $uid,
            'ProfileItem'=>$ProfileItem,
        ];
        return Http::post($url,json_encode($data));
    }

    /**
     * 获取资料
     * @param $uid
     * @param $TagList
     * @return mixed|string
     */
//    function portrait_get($uid=[],$TagList=[])
//    {
//        $url=$this->url.'/v4/profile/portrait_get?sdkappid='.$this->app_id.'&identifier='.$this->identifier.'&usersig='.$this->usersig.'&random='.mt_rand(0,4294967295).'&contenttype=json';
//        $data=[
//            'To_Account'=> explode(',',implode(',', $uid)),
//            'TagList'=>$TagList,
//        ];
//        return Http::post($url,json_encode($data));
//    }
}